package services

import (
	"time"
	"uwbwebapp/pkg/biz"
	"uwbwebapp/pkg/entities"
	"uwbwebapp/pkg/tools"

	"github.com/kataras/iris/v12"
)

func WSGenerateUWBTerminalRecordReport(ctx iris.Context) {
	message := WebServiceMessage{Message: "OK", StatusCode: 200}
	var err error
	swimmerId := ctx.FormValue("swimmer_id")
	beginDate := ctx.FormValue("begin_date")
	endDate := ctx.FormValue("end_date")
	var date entities.BetweenDatetime
	var reports []entities.SwimmerUWBTerminalRecordReport

	date.BeginDatetime, err = time.Parse(tools.GOOGLE_DATETIME_FORMAT_NO_TIME, beginDate)
	if err == nil {
		date.EndDatetime, err = time.Parse(tools.GOOGLE_DATETIME_FORMAT_NO_TIME, endDate)
		if err == nil {
			reports, err = biz.GenerateUWBTerminalRecordReport(swimmerId, date)
			if err == nil {
				message.Message = reports
			}
		}
	}
	if err != nil {
		message.Message = err
		message.StatusCode = 500
	}

	ctx.StatusCode(message.StatusCode)
	ctx.JSON(message)
}
